package com.ant.myagile



import grails.converters.JSON
import grails.plugins.springsecurity.Secured

import com.ant.myagile.backlog.RetrospectiveResult
import com.ant.myagile.backlog.Sprint
import com.ant.myagile.backlog.UserStory


class HomeController {
	def springSecurityService
	ChartService chartService=new ChartService()
	@Secured(['ROLE_USER', 'ROLE_ADMIN', 'ROLE_GUEST'])
	def index() {
	}

	def getIssuesBySprintId(Long id) {
		if (id) {
			def sprint = Sprint.get(id)

			if (params.sprint) {
				if (sprint.backlog) {
					params.result = sprint.backlog
					params.message = "Success"
				}
			} else {
				params.message = "Sprint does not exist."
			}
		} else {
			params.message = "Please input Sprint Id."
		}
		render params as JSON
	}

	def getTeams() {
		def member = springSecurityService.currentUser
		def id = params.projectId
		if (id) {
			def project = Project.get(id)
			if (project && project.owner == member) {
				def tpList = project.teamProjects.asList()
				def teams = new LinkedList()
				for (a in tpList) {
					teams.add(a.team)
				}
				if (teams.size() > 0) {
					teams.sort()
					params.result = teams
					params.message = "Success"
				} else {
					params.message = "No team in project."
				}
			} else if (project) {
				def tpList = project.teamProjects.asList()
				def teams = new LinkedList()
				for (tp in tpList) {
					def tmList = tp.team.teamMembers
					if (tmList) {
						for (tm in tmList) {
							if (tm.member == member) {
								teams.add(tp.team)
							}
						}
					}
				}
				if (teams.size() > 0) {
					teams.sort()
					params.result = teams
					params.message = "Success"
				} else {
					params.message = "No team in project."
				}
			} else {
				params.message = "Project Id#" + id + " does not exist."
			}
		} else {
			params.message = "Please input Project Id."
		}
		render params as JSON
	}

	def getSprints() {
		if (params.projectId && params.teamId) {
			def p = Project.get(params.projectId)
			def t = Team.get(params.teamId)
			def tp = Team_Project.find() {project == p && team == t}
			def beginDate = tp.startDate
			params.result = Sprint.findAll(sort: "id", order: "desc") {team == t && dateStart >= beginDate}
			params.message = "Success"
		} else {
			params.message = "ProjectId or TeamId is invalid."
		}
		render params as JSON
	}

	def listProjects() {
		def member = springSecurityService.currentUser
		def tm = Team_Member.findAllByMember(member)
		def teams = new LinkedList()

		if (tm) {
			for (t in tm) {
				teams.add(t.team);
			}
		}

		def projects = new HashSet()
		def p1 = Project.findAllByOwner(member);
		def p2 = new HashSet()
		for (t in teams) {
			if (t.teamProjects) {
				for (tp in t.teamProjects) {
					p2.add(tp.project)
				}
			}
		}

		for (p in p1) {
			projects.add(p)
		}

		for (p in p2) {
			projects.add(p)
		}

		if (projects.size() > 0) {
			params.result = projects
			params.message = "Success"
		} else {
			params.message = "No team loaded."
		}
		render params as JSON
	}

	def getUserStories() {
		def id = params.projectId
		if (id) {
			def backlog = UserStory.findAll(sort: "id", order: "desc") {project == Project.get(id)}
			if (backlog) {
				params.result = backlog
				params.message = "Success"
			} else {
				params.message = "Project#" + id + " has no user stories."
			}
		} else {
			params.message = "Please input Project Id for loading user storiese."
		}
		render params as JSON
	}

	def updateUserStory() {
		def userStory = UserStory.get(params.id)

		userStory.name = params.name
		userStory.description = params.description
		userStory.value = params.value.toInteger()
		userStory.status = params.status
		userStory.save(flush:true)
		params.message = "success"
		render params as JSON
	}

	def createUserStory() {
		params.project = Project.get(params.project)
		def userStory = new UserStory(params)
		userStory.save(flush: true)
		render userStory as JSON
	}

	def deleteUserStory() {
		UserStoryService uss= new UserStoryService()
		def userStory = UserStory.get(params.userStoryId)
		if (userStory) {
			def member = springSecurityService.currentUser
			uss.informToMemberWhenDelete(member,params.userStoryId)
			params.message = "User story deleted."
			userStory.delete();
		} else {
			params.message = "User story not found."
		}
		render params as JSON
	}

	def showRetrospective(){
		if(params.id){
			def result = RetrospectiveResult.findAllBySprint(Sprint.get(params.id))
			render result as JSON
		}
	}

	//Chart functions
	def getChartData(){

		def sprintID= params.id
		def sprint=Sprint.get(sprintID)
		def data=chartService.getDataChartBySprint(sprintID)
		sleep(1000)
		render ([
			dList: data.dList,
			pList: data.pList,
			iList: data.iList,
			bList: data.bList,
			sprint: sprint
		] as JSON)
	}
}
